package com.zhaimen.module.product.service.homelink;

import com.zhaimen.framework.common.enums.CommonStatusEnum;
import com.zhaimen.framework.common.pojo.CommonResult;
import com.zhaimen.framework.common.pojo.PageResult;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkCreateReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkPageReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkUpdateReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkUpdateStatusReqVO;
import com.zhaimen.module.product.convert.homelink.HomeLinkConvert;
import com.zhaimen.module.product.dal.dataobject.homelink.HomeLinkDO;
import com.zhaimen.module.product.dal.mysql.homelink.HomeLinkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;

import static com.zhaimen.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zhaimen.module.product.enums.ErrorCodeConstants.*;

@Service
public class HomeLinkServiceImpl implements HomeLinkService{
    @Resource
    private HomeLinkMapper homeLinkMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createHomeLink(HomeLinkCreateReqVO reqVO) {
        validHomeLinkCreateOrUpdate(null,reqVO.getName());
        //格式转化
        HomeLinkDO homeLinkDO=HomeLinkConvert.INSTANCE.convert01(reqVO);
        homeLinkDO.setCommonStatus(CommonStatusEnum.DISABLE.getStatus());
        homeLinkDO.setDeleted(false);
        homeLinkMapper.insert(homeLinkDO);
        return homeLinkDO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateHomeLink(HomeLinkUpdateReqVO reqVO) {
        validHomeLinkCreateOrUpdate(reqVO.getId(),reqVO.getName());
       HomeLinkDO homeLinkDO= HomeLinkConvert.INSTANCE.convert01(reqVO);
       homeLinkMapper.updateById(homeLinkDO);
    }

    @Override
    public HomeLinkDO getHomeLinkById(Long id) {
        validIdForExist(id);
        HomeLinkDO homeLinkDO = homeLinkMapper.selectById(id);
        return homeLinkDO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateHomeLinkStatus(HomeLinkUpdateStatusReqVO reqVO) {
        HomeLinkDO homeLinkDO = homeLinkMapper.selectById(reqVO.getId());
        homeLinkDO.setCommonStatus(reqVO.getCommonStatus());
        homeLinkMapper.updateById(homeLinkDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHomeLinkById(Long id) {
        validIdForExist(id);
        homeLinkMapper.deleteById(id);
    }

    @Override
    public PageResult<HomeLinkDO> getHomeLinkPage(HomeLinkPageReqVO reqVO) {
        return homeLinkMapper.selectPage(reqVO);
    }

    @Override
    public List<HomeLinkDO> getHomeLinkAll() {
        List<HomeLinkDO> homeLinkDOS = homeLinkMapper.selectByCommonStatus(CommonStatusEnum.ENABLE.getStatus());
        return homeLinkDOS;
    }

    private void validHomeLinkCreateOrUpdate(Long id, String name) {
        validateNameForUnique(name,id);
        validIdForExist(id);
    }

    private void validateNameForUnique(String name, Long id) {
        if (name == null){
            return;
        }
        HomeLinkDO homeLinkDO = homeLinkMapper.selectByName(name);
        if(homeLinkDO==null){
            return;
        }
        if(id==null){
            exception(HOMELINK_NAME_EXIST);
        }
        if(name.equals(homeLinkDO.getName())){
            exception(HOMELINK_NAME_EXIST);
        }
    }

    private void validIdForExist(Long id) {
        if(id==null){
            return;
        }
        HomeLinkDO homeLinkDO = homeLinkMapper.selectById(id);
        if(homeLinkDO==null){
            throw exception(HOMELINK_NOT_EXIST);
        }

    }



}
